package com.lk.org.controller;

import com.lk.basic.util.AjaxResult;
import com.lk.basic.util.PageList;
import com.lk.org.domain.Department;
import com.lk.org.query.DepartmentQuery;
import com.lk.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")// ：打在Controller对Controller做描述
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //增&改
    @PutMapping
    @ApiOperation(value = "新增&修改" )// ：打在方法上，对方法做描述
    public AjaxResult save(@RequestBody Department department){
        try {
            if(department.getId() != null){
                departmentService.update(department);
            }else {
                departmentService.insert(department);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
        return AjaxResult.me();
    }

    //批量删
    @PatchMapping
    @ApiOperation(value = "批量删除" )// ：打在方法上，对方法做描述
    public AjaxResult delAll(@RequestBody List<Long> ids){
        try {
            departmentService.delAll(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
        return AjaxResult.me();
    }
    //删
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )// ：打在方法上，对方法做描述
    public AjaxResult delById(@ApiParam(required = true) @PathVariable("id") Long id){
        try {
            departmentService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
        return AjaxResult.me();
    }

    //查单个
    @GetMapping("/{id}")
    @ApiOperation(value = "单个查询" )// ：打在方法上，对方法做描述
    public AjaxResult selById(@ApiParam(required = true) @PathVariable("id") Long id){
        try {
            Department department = departmentService.selById(id);
            return AjaxResult.me().setResult(department);

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }
    //查所有
    @GetMapping
    @ApiOperation(value = "查询所有" )// ：打在方法上，对方法做描述
    public AjaxResult selAll(){
        try {
            List<Department> departments = departmentService.selAll();
            return AjaxResult.me().setResult(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }

    }
    //分页
    @PostMapping
    @ApiOperation(value = "分页" )// ：打在方法上，对方法做描述
    public AjaxResult pageList(@RequestBody DepartmentQuery query){
        try {
            PageList<Department> pageList = departmentService.queryPage(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }
    @GetMapping("/tree")
    public AjaxResult getDepartmentTree(){
        try {
            List<Department> departments = departmentService.getDepartmentTree();
            return AjaxResult.me().setResult(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }
}